﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

using System.Linq;
using System.Diagnostics;
using Saluse.MediaKit;

namespace OpenLightGroup.Visualizers
{
    public class Orchestrator
        : FrameworkElement
    {

        public event EventHandler SoundBiteUpdated;

        public static readonly DependencyProperty MediaStreamProperty =
            DependencyProperty.Register("MediaStream", typeof(IRawMediaStreamSource), 
            typeof(Orchestrator), new PropertyMetadata(null));


        public Orchestrator()
        {

        }

        public IRawMediaStreamSource MediaStream
        {
            get { return (IRawMediaStreamSource)GetValue(MediaStreamProperty); }
            set
            {
                SetValue(MediaStreamProperty, value);
               
                value.SampleReady +=
                    new Saluse.MediaKit.Delegates
                        .SampleReadyDelegate(MediaStream_SampleReady);
            }
        }

        public SoundBite CurrentSoundBite
        {
            get;
            private set;

        }

        protected virtual void MediaStream_SampleReady(object sender, short[] samples)
        {

            Dispatcher.BeginInvoke(() =>
            {
                this.CurrentSoundBite = new SoundBite
                    (this.MediaStream.SamplesPerSecond, samples);

                if (SoundBiteUpdated != null)
                    this.SoundBiteUpdated(this, new EventArgs());
            });


        }


    }
}
